#include "../ModelEditorConfig.h"
#include "ResourceDirComponent.h"
#include "MainFrame.h"

ModelEditorMainFrame::ModelEditorMainFrame()
{
	m_pToolbar = new ToolbarListener(this);

	m_pD3DCanvas = new MainViewComponent(this);

	m_pLeftComponent = new TabbedComponent(TabbedButtonBar::TabsAtLeft);
    m_pLeftComponent->setTabBarDepth(30);
    m_pLeftComponent->addTab(L"Resource Explorer", Colours::lightgrey, new ResourceDirComponent(this), true);
    m_pLeftComponent->setCurrentTabIndex(0);


	m_pRightComponent = new TabbedComponent(TabbedButtonBar::TabsAtRight);
    m_pRightComponent->setTabBarDepth(30);
    m_pRightComponent->addTab(L"Model Details", Colours::lightgrey, NULL/*new MaterialPreviewPage(this)*/, true);
    m_pRightComponent->setCurrentTabIndex(0);

	m_bShowLeft = m_bShowRight = m_bShowBottom = true;
}



void ModelEditorMainFrame::CreateLayout()
{
	verticalLayout.setItemLayout(0, 30, -0.5, -0.25);		// width of the Left list must be
															// between 30 pixels and 50%, preferably 25%
    verticalLayout.setItemLayout(1, 8, 8, 8);				// the vertical divider drag-bar thing is always 8 pixels wide
    verticalLayout.setItemLayout(2, 350, -1.0, -0.75);		// the components on the middle must be
															// at least 350 pixels wide, preferably 75% of the total width
    verticalLayout.setItemLayout(3, 8, 8, 8);				// the vertical divider drag-bar thing is always 8 pixels wide
    verticalLayout.setItemLayout(4, 30, -0.3, -0.25);		// the components on the right must be
                                                            // between 30 pixels and 30%, preferably 25% of the total width

    verticalDividerBarLeft = new StretchableLayoutResizerBar(&verticalLayout, 1, true);
	verticalDividerBarRight = new StretchableLayoutResizerBar(&verticalLayout, 3, true);

}

void ModelEditorMainFrame::resized(int width, int height)
{
	m_width = width;
	m_height = height;

	const int ToolBarHeight = 32;
	m_pToolbar->setBounds(0, 0, width, ToolBarHeight);
	height -= ToolBarHeight;

	// lay out the vertical divider..
    Component* vcomps[] = { m_pLeftComponent, verticalDividerBarLeft, m_pD3DCanvas, verticalDividerBarRight, m_pRightComponent };

    verticalLayout.layOutComponents(vcomps, 5, 0, ToolBarHeight, width, height,
                                    false,     // lay out side-by-side
                                    true);     // resize the components' heights as well as widths


	int Panel_left = verticalLayout.getItemCurrentPosition(1);
	if( m_bShowLeft )
		verticalDividerBarLeft->setVisible(true);
	else
	{
		Panel_left = 0;
		verticalDividerBarLeft->setVisible(false);
	}
	m_pLeftComponent->setBounds(0, ToolBarHeight, Panel_left, height);

	int Panel_right = verticalLayout.getItemCurrentPosition(3)+8;
	if( m_bShowRight )
		verticalDividerBarRight->setVisible(true);
	else
	{
		Panel_right = width;
		verticalDividerBarRight->setVisible(false);
	}
	m_pRightComponent->setBounds(Panel_right, ToolBarHeight, width-Panel_right, height);
	
	m_pD3DCanvas->setBounds(m_pLeftComponent->getWidth()+8, ToolBarHeight, width-m_pLeftComponent->getWidth()-8-m_pRightComponent->getWidth()-8, height);
 }

void ModelEditorMainFrame::HideLeftPanel()
{
	m_bShowLeft = false;
	resized(m_width, m_height);
}

void ModelEditorMainFrame::HideRightPanel()
{
	m_bShowRight = false;
	resized(m_width, m_height);
}

void ModelEditorMainFrame::HideBottomPanel()
{
	m_bShowBottom = false;
	m_pD3DCanvas->resized();
}

void ModelEditorMainFrame::ShowLeftPanel()
{
	m_bShowLeft = true;
	resized(m_width, m_height);
}

void ModelEditorMainFrame::ShowRightPanel()
{
	m_bShowRight = true;
	resized(m_width, m_height);
}

void ModelEditorMainFrame::ShowBottomPanel()
{
	m_bShowBottom = true;
	m_pD3DCanvas->resized();
}